package section_06.deadLock;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class FixedDiningPhilosophers {
	public static void main(String[] args) throws InterruptedException {
		int ponder = 5;
		int size = 5;
		ExecutorService exec = Executors.newCachedThreadPool();
		Chopstick[] sticks = new Chopstick[size];
		for (int i = 0; i < size; i++)
			sticks[i] = new Chopstick();
		for (int i = 0; i < size; i++) {
			if (i < (size - 1))
				//except the last philosopher, others first hold the right chopstick, then hold the left
				exec.execute(new Philosopher(sticks[i], sticks[i + 1], i, ponder));
			else
				//the last philosopher first hold the left chopstick, then hold the right
				exec.execute(new Philosopher(sticks[0], sticks[i], i, ponder));
			TimeUnit.SECONDS.sleep(5);
			exec.shutdownNow();
		}
	}
}
